home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / gen / SLList.ccP < prev    next >
Text File  |  1993-11-18  |  5KB  |  293 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. // WARNING: This file is obsolete.  Use ../SLList.cc, if you can.
  3. /* 
  4. Copyright (C) 1988 Free Software Foundation
  5.     written by Doug Lea (dl@rocky.oswego.edu)
  6.  
  7. This file is part of the GNU C++ Library.  This library is free
  8. software; you can redistribute it and/or modify it under the terms of
  9. the GNU Library General Public License as published by the Free
  10. Software Foundation; either version 2 of the License, or (at your
  11. option) any later version.  This library is distributed in the hope
  12. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  13. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14. PURPOSE.  See the GNU Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #ifdef __GNUG__
  21. #pragma implementation
  22. #endif
  23. #include <limits.h>
  24. #include <stream.h>
  25. #include <builtin.h>
  26. #include "<T>.SLList.h"
  27.  
  28. void <T>SLList::error(const char* msg)
  29. {
  30.   (*lib_error_handler)("SLList", msg);
  31. }
  32.  
  33. int <T>SLList::length()
  34. {
  35.   int l = 0;
  36.   <T>SLListNode* t = last;
  37.   if (t != 0) do { ++l; t = t->tl; } while (t != last);
  38.   return l;
  39. }
  40.  
  41. <T>SLList::<T>SLList(const <T>SLList& a)
  42. {
  43.   if (a.last == 0)
  44.     last = 0;
  45.   else
  46.   {
  47.     <T>SLListNode* p = a.last->tl;
  48.     <T>SLListNode* h = new <T>SLListNode(p->hd);
  49.     last = h;
  50.     for (;;)
  51.     {
  52.       if (p == a.last)
  53.       {
  54.         last->tl = h;
  55.         return;
  56.       }
  57.       p = p->tl;
  58.       <T>SLListNode* n = new <T>SLListNode(p->hd);
  59.       last->tl = n;
  60.       last = n;
  61.     }
  62.   }
  63. }
  64.  
  65. <T>SLList& <T>SLList::operator = (const <T>SLList& a)
  66. {
  67.   if (last != a.last)
  68.   {
  69.     clear();
  70.     if (a.last != 0)
  71.     {
  72.       <T>SLListNode* p = a.last->tl;
  73.       <T>SLListNode* h = new <T>SLListNode(p->hd);
  74.       last = h;
  75.       for (;;)
  76.       {
  77.         if (p == a.last)
  78.         {
  79.           last->tl = h;
  80.           break;
  81.         }
  82.         p = p->tl;
  83.         <T>SLListNode* n = new <T>SLListNode(p->hd);
  84.         last->tl = n;
  85.         last = n;
  86.       }
  87.     }
  88.   }
  89.   return *this;
  90. }
  91.  
  92. void <T>SLList::clear()
  93. {
  94.   if (last == 0)
  95.     return;
  96.  
  97.   <T>SLListNode* p = last->tl;
  98.   last->tl = 0;
  99.   last = 0;
  100.  
  101.   while (p != 0)
  102.   {
  103.     <T>SLListNode* nxt = p->tl;
  104.     delete(p);
  105.     p = nxt;
  106.   }
  107. }
  108.  
  109.  
  110. Pix <T>SLList::prepend(<T&> item)
  111. {
  112.   <T>SLListNode* t = new <T>SLListNode(item);
  113.   if (last == 0)
  114.     t->tl = last = t;
  115.   else
  116.   {
  117.     t->tl = last->tl;
  118.     last->tl = t;
  119.   }
  120.   return Pix(t);
  121. }
  122.  
  123.  
  124. Pix <T>SLList::prepend(<T>SLListNode* t)
  125. {
  126.   if (t == 0) return 0;
  127.   if (last == 0)
  128.     t->tl = last = t;
  129.   else
  130.   {
  131.     t->tl = last->tl;
  132.     last->tl = t;
  133.   }
  134.   return Pix(t);
  135. }
  136.  
  137.  
  138. Pix <T>SLList::append(<T&> item)
  139. {
  140.   <T>SLListNode* t = new <T>SLListNode(item);
  141.   if (last == 0)
  142.     t->tl = last = t;
  143.   else
  144.   {
  145.     t->tl = last->tl;
  146.     last->tl = t;
  147.     last = t;
  148.   }
  149.   return Pix(t);
  150. }
  151.  
  152. Pix <T>SLList::append(<T>SLListNode* t)
  153. {
  154.   if (t == 0) return 0;
  155.   if (last == 0)
  156.     t->tl = last = t;
  157.   else
  158.   {
  159.     t->tl = last->tl;
  160.     last->tl = t;
  161.     last = t;
  162.   }
  163.   return Pix(t);
  164. }
  165.  
  166. void <T>SLList::join(<T>SLList& b)
  167. {
  168.   <T>SLListNode* t = b.last;
  169.   b.last = 0;
  170.   if (last == 0)
  171.     last = t;
  172.   else if (t != 0)
  173.   {
  174.     <T>SLListNode* f = last->tl;
  175.     last->tl = t->tl;
  176.     t->tl = f;
  177.     last = t;
  178.   }
  179. }
  180.  
  181. Pix <T>SLList::ins_after(Pix p, <T&> item)
  182. {
  183.   <T>SLListNode* u = (<T>SLListNode*)p;
  184.   <T>SLListNode* t = new <T>SLListNode(item);
  185.   if (last == 0)
  186.     t->tl = last = t;
  187.   else if (u == 0) // ins_after 0 means prepend
  188.   {
  189.     t->tl = last->tl;
  190.     last->tl = t;
  191.   }
  192.   else
  193.   {
  194.     t->tl = u->tl;
  195.     u->tl = t;
  196.     if (u == last) 
  197.       last = t;
  198.   }
  199.   return Pix(t);
  200. }
  201.  
  202.  
  203. void <T>SLList::del_after(Pix p)
  204. {
  205.   <T>SLListNode* u = (<T>SLListNode*)p;
  206.   if (last == 0 || u == last) error("cannot del_after last");
  207.   if (u == 0) u = last; // del_after 0 means delete first
  208.   <T>SLListNode* t = u->tl;
  209.   if (u == t)
  210.     last = 0;
  211.   else
  212.   {
  213.     u->tl = t->tl;
  214.     if (last == t)
  215.       last = u;
  216.   }
  217.   delete t;
  218. }
  219.  
  220. int <T>SLList::owns(Pix p)
  221. {
  222.   <T>SLListNode* t = last;
  223.   if (t != 0 && p != 0)
  224.   {
  225.     do
  226.     {
  227.       if (Pix(t) == p) return 1;
  228.       t = t->tl;
  229.     } while (t != last);
  230.   }
  231.   return 0;
  232. }
  233.  
  234. <T> <T>SLList::remove_front()
  235. {
  236.   if (last == 0) error("remove_front of empty list");
  237.   <T>SLListNode* t = last->tl;
  238.   <T> res = t->hd;
  239.   if (t == last)
  240.     last = 0;
  241.   else
  242.     last->tl = t->tl;
  243.   delete t;
  244.   return res;
  245. }
  246.  
  247. int <T>SLList::remove_front(<T>& x)
  248. {
  249.   if (last == 0)
  250.     return 0;
  251.   else
  252.   {
  253.     <T>SLListNode* t = last->tl;
  254.     x = t->hd;
  255.     if (t == last)
  256.       last = 0;
  257.     else
  258.       last->tl = t->tl;
  259.     delete t;
  260.     return 1;
  261.   }
  262. }
  263.  
  264.  
  265. void <T>SLList::del_front()
  266. {
  267.   if (last == 0) error("del_front of empty list");
  268.   <T>SLListNode* t = last->tl;
  269.   if (t == last)
  270.     last = 0;
  271.   else
  272.     last->tl = t->tl;
  273.   delete t;
  274. }
  275.  
  276. int <T>SLList::OK()
  277. {
  278.   int v = 1;
  279.   if (last != 0)
  280.   {
  281.     <T>SLListNode* t = last;
  282.     long count = LONG_MAX;      // Lots of chances to find last!
  283.     do
  284.     {
  285.       count--;
  286.       t = t->tl;
  287.     } while (count > 0 && t != last);
  288.     v &= count > 0;
  289.   }
  290.   if (!v) error("invariant failure");
  291.   return v;
  292. }
  293.